<template>
	<div class="sys-app-container">
		<el-dialog v-model="isShowDialog" draggable width="600px" :destroy-on-close="true">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>
					<span> {{ title }} </span>
				</div>
			</template>
			<el-form :model="ruleForm" ref="ruleFormRef" size="default" label-width="80px" :rules="rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label="$t('3r2FlPOWtIwAoavmSIfU')" prop="name">
							<el-input v-model="ruleForm.name" :placeholder="$t('y25yDGqSsWbi5Cn6OEo32')" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label="$t('xjNlStOppRQxMs2bC5ps')" prop="code">
							<el-input v-model="ruleForm.code" :placeholder="$t('xiEEkSunKpXOd16Scrw')" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label="$t('kuhaomW54NWjkDOuzNp')"  prop="icon">
							<IconSelector v-model="ruleForm.icon" :placeholder="$t('o5gG7rTfqdAGvdry9c7I')" type="all" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item :label="$t('ivdjt7Bot7XrcZX12b3a')" prop="sort">
							<el-input-number v-model="ruleForm.sort" :placeholder="$t('oMjm9D4Boupe4GadoKqJq')" class="w100" />
						</el-form-item>
					</el-col>
					
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="isShowDialog = false" size="default">{{ $t('xjdBVqTHmklt0GuDv3mw') }}</el-button>
					<el-button type="primary" @click="submit(ruleFormRef)" size="default">{{ $t('ztv2Tafnuu9rusmujXn7t') }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<script lang="ts">
import { defineComponent, reactive, toRefs, ref } from 'vue';
import type { FormInstance, FormRules } from 'element-plus';
import { addNewApp, editApp } from '/@/api/system/app';
import IconSelector from '/@/components/IconSelector/index.vue';
interface formType {
	isShowDialog: boolean;
	ruleForm: ruleFormType;
}
type ruleFormType = {
	code: string | number;
	name: string | number;
	active: string | number;
	sort: string | number;
	id?: number | string;
	icon?: string
};
export default defineComponent({
	emits: ['updateData'],
	components: { IconSelector },
	setup(props, ctx) {
		const state = reactive<formType>({
			isShowDialog: false,
			ruleForm: {
				code: '',
				name: '',
				active: 'N',
				sort: 100,
				id: '',
				icon: '',
			},
		});
		// 表单标题
		const title = ref('');

		const ruleFormRef = ref();
		const rules = reactive<FormRules>({
			name: [{ required: true, message: window.$t('2fyVwr7qOqKkx2Opz6Th7'), trigger: 'blur' }],
			code: [{ required: true, message: window.$t('j5Ed_6n97ts8IKh0Sxam'), trigger: 'blur' }],
		});
		//打开表单弹窗
		const openForm = (data?: ruleFormType) => {
			// 初始化数据
			state.ruleForm = {
				code: '',
				name: '',
				active: 'N',
				sort: 100,
				id: '',
				icon: '',
			};
			title.value = data ? window.$t('uKl2SSk7U3Fc65qzVuawm') : window.$t('hCJ9xuWdiC_7V4265YWz');

			if (data) {
				state.ruleForm.id = data.id;
				state.ruleForm.code = data.code;
				state.ruleForm.name = data.name;
				state.ruleForm.active = data.active;
				state.ruleForm.sort = data.sort;
				state.ruleForm.icon = data.icon || ''
			}
			state.isShowDialog = true;
		};
		// 提交
		const submit = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.validate(async (valid) => {
				if (valid) {
					try {
						// 有id是修改，无id是新增
						if (state.ruleForm.id === '') {
							let params = JSON.parse(JSON.stringify(state.ruleForm));
							delete params.id;
							let res = await addNewApp(params);
						} else {
							let params = JSON.parse(JSON.stringify(state.ruleForm));
							let res = await editApp(params);
						}
						ctx.emit('updateData');
					} catch (err) {
					} finally {
						state.isShowDialog = false;
					}
				} else {
					console.log('error submit!');
					return false;
				}
			});
		};
		return {
			...toRefs(state),
			submit,
			openForm,
			rules,
			ruleFormRef,
			title,
		};
	},
});
</script>
